//
//  MyAdvisoryOrderCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/28.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

protocol MyAdvisoryOrderCellDelegate: NSObjectProtocol {
    func orderCell(didTappedDelete cell: MyAdvisoryOrderCell)
    func orderCell(didTappedRefound cell: MyAdvisoryOrderCell)
}

class MyAdvisoryOrderCell: UITableViewCell {
    
    @IBOutlet weak var businessTypeLabel: UILabel!{
        didSet{
            businessTypeLabel.layer.cornerRadius = 5
            businessTypeLabel.layer.masksToBounds = true
        }
    }
    
    weak var delegate: MyAdvisoryOrderCellDelegate?

    override func awakeFromNib() {
        super.awakeFromNib()
        backgroundColor = .clear
        contentView.backgroundColor = .clear
    }
    
    @IBOutlet private weak var containerView: UIView! {
        didSet {
            containerView.layer.cornerRadius = 8
            containerView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var avatarView: UIImageView! {
        didSet {
            avatarView.contentMode = .scaleAspectFill
            avatarView.layer.cornerRadius = avatarView.height * 0.5
            avatarView.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var nameLabel: UILabel!
    
    @IBOutlet private weak var departmentLabel: UILabel! {
        didSet {
            departmentLabel.textColor = .mainColor
            departmentLabel.font = .regular_14
        }
    }
    
    @IBOutlet private weak var statusLabel: UILabel!
    
    @IBOutlet private weak var timeLabel: UILabel!
    
    @IBOutlet private weak var actionBtn: UIButton! {
        didSet {
            actionBtn.setTitle("删除订单", for: .normal)
            actionBtn.setTitleColor(.white, for: .normal)
            actionBtn.setTitleColor(.main, for: .highlighted)
            actionBtn.setBackgroundImage(UIImage(color: .mainColor), for: .normal)
            actionBtn.setBackgroundImage(UIImage(color: .mainLight), for: .highlighted)
            actionBtn.layer.cornerRadius = actionBtn.height / 2
            actionBtn.layer.masksToBounds = true
        }
    }
    
    @IBOutlet private weak var splitLine: UIView!
    
    private var viewModel: MyAdvisoryOrderRowViewModel?
    
    @IBAction private func clickedAction(_ sender: UIButton) {
        guard let vm = viewModel else { return }
        switch vm.status {
        case .waitPay:
            delegate?.orderCell(didTappedDelete: self)
        case .processing:
            delegate?.orderCell(didTappedRefound: self)
        default: break
        }
    }
    
    func configure(with vm: MyAdvisoryOrderRowViewModel) {
        viewModel = vm
        avatarView.kf.setImage(with: URL(string: vm.url),
                               placeholder: UIImage(named: "avatar_default"))
        nameLabel.text = vm.doctorName
        departmentLabel.text = vm.departmentName
        statusLabel.text = vm.status.description()
        statusLabel.textColor = vm.status.statusColor()
        timeLabel.attributedText = vm.tipText
        timeLabel.isHidden = vm.status != .waitPay
        
        switch vm.status {
        case .processing:
            splitLine.isHidden = vm.canRefund == 0
            actionBtn.isHidden = vm.canRefund == 0
            actionBtn.setTitle("退款", for: .normal)
        case .waitPay:
            splitLine.isHidden = false
            actionBtn.isHidden = false
            actionBtn.setTitle("删除订单", for: .normal)
        default:
            actionBtn.isHidden = true
            splitLine.isHidden = true
        }
        ///业务类型-1:咨询2:复诊
        if vm.model.businessType == 1 {
            businessTypeLabel.text = "咨询"
        }else if vm.model.businessType == 2{
            businessTypeLabel.text = "复诊"
        }
        
    }
}
